package yr.website

import grails.converters.JSON
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.imageio.ImageIO
import java.text.SimpleDateFormat

class UserController {
    def userService
    static final Logger logger = LoggerFactory.getLogger(UserController.class)

    def index() {
        render(view: "/index")
    }

    def solution() {
        render(view: "/solution")
    }

    def about() {
        render(view: "/about_us")
    }

    def join() {
//        def list = Jobb.list()
        def list = Jobb.findAllByState(true)
        render(view: "/join_us", model: [data: list])
    }

    /**
     * 验证码
     * @return
     */
    def getCode() {
        response.setHeader("Expires", "-1")
        response.setHeader("Cache-Control", "no-cache")
        response.setHeader("Pragma", "-1")
        userService.get()
        session.setAttribute("seesionCode", userService.getStr())
        ImageIO.write(userService.getImage(), "jpg", response.getOutputStream())
    }

    /**
     * 验证码验证
     * @return
     */
    def checkCode() {
        def data = [:]
        def code = "${params.code}"
        def sessionCode = session.getAttribute("seesionCode")
        def str = userService.checkCode(sessionCode, code)
        data << [data: str]
        render data as JSON
    }

    def add() {
        def data = [:]
        def username = "${params.username}"
        def phone = "${params.phone}"
        def email = "${params.email}"
        def mess = "${params.mess}"
        def user = new User()
        user.name = username
        user.phone = phone
        user.email = email
        user.message = mess
        def flag = userService.save(user)
        data << [data: flag]
        render data as JSON
    }



    def getAll() {
        def data = [:]
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd")
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def startime = "${params.recordDate1 ?: ''}"
        def endtime = "${params.recordDate2 ?: ''}"
        def a = User.createCriteria()
        def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
            and {
                if (!startime.isEmpty()) {
                    ge("recordDate", new Date(Long.parseLong(startime)))
                }
                if (!endtime.isEmpty()) {
                    def start = sdf2.format(new Date(Long.parseLong(endtime)))
                    Date end = sdf1.parse(start + " 23:59:59")
                    le("recordDate", end)
                }
            }
            order("recordDate", "desc")
        }
        def b = User.createCriteria()
        def length = b.get {
            and {
                if (!startime.isEmpty()) {
                    ge("recordDate", new Date(Long.parseLong(startime)))
                }
                if (!endtime.isEmpty()) {
                    def start = sdf2.format(new Date(Long.parseLong(endtime)))
                    Date end = sdf1.parse(start + " 23:59:59")
                    le("recordDate", end)
                }
            }
            projections {
                rowCount()
            }
        }
        if (list) {
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
        } else {
            data << [statuCode: "1", errMsg: "1"]
        }
        render data as JSON

    }
}
